package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.data.entity.RiskUserCardLog;
import com.vcc.data.mapper.RiskUserCardLogMapper;
import com.vcc.service.boss.response.risk.RiskUserCardLogVoBoss;
import com.vcc.service.service.RiskUserCardLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.CardTypeTableDef.CARD_TYPE;
import static com.vcc.data.entity.table.RiskUserCardLogTableDef.RISK_USER_CARD_LOG;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class RiskUserCardLogServiceImpl extends ServiceImpl<RiskUserCardLogMapper, RiskUserCardLog> implements RiskUserCardLogService {


    @Override
    public boolean completeTransactionLog(Long id, Integer status, String responseMessage) {
        return updateChain()
                .set(RiskUserCardLog::getStatus, status)
                .set(RiskUserCardLog::getRemark, responseMessage)
                .set(RiskUserCardLog::getCompleteDate, new Date())
                .set(RiskUserCardLog::getDisposeStatus, status == 0 ? 0 : 1)
                .eq(RiskUserCardLog::getId, id)
                .update();
    }

    @Override
    public boolean completeTransactionLog(List<Long> ids, Integer status, String responseMessage) {
        return updateChain()
                .set(RiskUserCardLog::getStatus, status)
                .set(RiskUserCardLog::getRemark, responseMessage)
                .set(RiskUserCardLog::getCompleteDate, new Date())
                .set(RiskUserCardLog::getDisposeStatus, status == 0 ? 0 : 1)
                .in(RiskUserCardLog::getId, ids)
                .update();
    }

    @Override
    public boolean completeTransactionLog(Long id, Integer status, String responseMessage, String functionName, Long operatorId) {
        return updateChain()
                .set(RiskUserCardLog::getStatus, status)
                .set(RiskUserCardLog::getRemark, responseMessage)
                .set(RiskUserCardLog::getCompleteDate, new Date())
                .set(RiskUserCardLog::getDisposeStatus, status == 0 ? 0 : 1)
                .set(RiskUserCardLog::getDisposeFunctionName, functionName)
                .set(RiskUserCardLog::getOperatorId, operatorId)
                .eq(RiskUserCardLog::getId, id)
                .update();
    }

    @Override
    public boolean completeTransactionLog(List<Long> ids, Integer status, String responseMessage, String functionName, Long operatorId) {
        return updateChain()
                .set(RiskUserCardLog::getStatus, status)
                .set(RiskUserCardLog::getRemark, responseMessage)
                .set(RiskUserCardLog::getCompleteDate, new Date())
                .set(RiskUserCardLog::getDisposeStatus, status == 0 ? 0 : 1)
                .set(RiskUserCardLog::getDisposeFunctionName, functionName)
                .set(RiskUserCardLog::getOperatorId, operatorId)
                .in(RiskUserCardLog::getId, ids)
                .update();
    }

    @Override
    public List<RiskUserCardLog> listByUserCardIdAndDisposeStatus(Long userCardId, Integer disposeStatus) {
        return queryChain()
                .eq(RiskUserCardLog::getUserCardId, userCardId)
                .eq(RiskUserCardLog::getDisposeStatus, disposeStatus)
                .list();
    }

    @Override
    public Page<RiskUserCardLogVoBoss> paging(Integer pagingNumber, Integer pageSize, Long userId, String channelName, String cardNumber, Long cardId, Integer status) {
        return queryChain()
                .select(RISK_USER_CARD_LOG.ALL_COLUMNS)
                .select(USER.ACCOUNT.as("userAccount"))
                .leftJoin(USER).on(USER.ID.eq(RISK_USER_CARD_LOG.USER_ID))
                .select(CARD_TYPE.SECTION_NO.as("sectionNo"))
                .leftJoin(CARD_TYPE).on(CARD_TYPE.ID.eq(RISK_USER_CARD_LOG.CARD_TYPE_ID))
                .select(BOSS_USER.EMAIL.as("operatorAccount"))
                .leftJoin(BOSS_USER).on(BOSS_USER.ID.eq(RISK_USER_CARD_LOG.OPERATOR_ID))
                .where(RISK_USER_CARD_LOG.USER_ID.eq(userId, userId != null && userId > 0))
                .where(RISK_USER_CARD_LOG.CHANNEL_NAME.eq(channelName, StringUtils.isNotBlank(channelName)))
                .where(RISK_USER_CARD_LOG.CARD_NUMBER.eq(cardNumber, StringUtils.isNotBlank(cardNumber)))
                .where(RISK_USER_CARD_LOG.CARD_TYPE_ID.eq(cardId, cardId != null && cardId > 0))
                .where(RISK_USER_CARD_LOG.STATUS.eq(status, status != null && status >= 0 && status <= 2))
                .orderBy(RiskUserCardLog::getCreateDate, false)
                .pageAs(Page.of(pagingNumber == null ? 1 : pagingNumber, pageSize == null ? 20 : pageSize), RiskUserCardLogVoBoss.class);
    }
    
}
